$(function () {

    window['fighting'] = {}
    var config = {
        mousemove_color: "#33EEF8",
        mouseout_color: "#DAEEF8",
    }
    //表格行，鼠标放上去变色
    $(".tr:odd").css("background", "#FFFCEA");
    $(".tr:odd").each(function () {
        $(this).hover(function () {
            $(this).css("background-color", "#FFE1FF");
        }, function () {
            $(this).css("background-color", "#FFFCEA");
        });
    });
    $(".tr:even").each(function () {
        $(this).hover(function () {
            $(this).css("background-color", "#FFE1FF");
        }, function () {
            $(this).css("background-color", "#fff");
        });
    });

    // /*ie6,7下拉框美化*/
    // if ($.browser.msie) {
    //     if ($.browser.version == '7.0' || $.browser.version == '6.0') {
    //         $('.select').each(function (i) {
    //             $(this).parents('.select_border,.select_containers').width($(this).width() + 5);
    //         });
    //
    //     }
    // }

    $('.UIbutton').mousedown(function (event) {


        this.style.backgroundColor = config['mousemove_color'];
    });
    $('.UIbutton').mouseout(function (event) {
        //alert(this.oldbg);
        this.style.backgroundColor = config['mouseout_color'];
    });


    function dateFormat(d, fmt) {
        var o = {
            "M+": d.getMonth() + 1, //月份   
            "d+": d.getDate(), //日   
            "h+": d.getHours(), //小时   
            "m+": d.getMinutes(), //分   
            "s+": d.getSeconds(), //秒   
            "q+": Math.floor((d.getMonth() + 3) / 3), //季度   
            "S": d.getMilliseconds() //毫秒   
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }

    function drawRect(dataset, ele) {

        $("#" + ele).empty();
        var d = [];
        for (var key in dataset) {

            d.push(dataset[key]['count']);
        }


        var w = 3800;
        var h = 200;
        var scale = d3.scale.linear();
        scale.domain([0, d3.max(d)])
        scale.range([0, 100]);
        var svg = d3.select("#" + ele)
            .append('svg')
            .attr("width", w)
            .attr("height", h);
        svg.selectAll("rect")
            .data(dataset)
            .enter()
            .append('rect')
            .attr("x", function (d, i) {
                return i * 50;
            })
            .attr("y", function (d, i) {
                return 100 - scale(d['count']);
            })
            .attr("width", 20)
            .attr("height", function (d, i) {
                return scale(d['count']);
            })
            .attr("fill", "lightblue");
        svg.selectAll("text")
            .data(dataset)
            .enter()
            .append('text')
            .attr("x", function (d, i) {
                return i * 50;
            })
            .attr("y", function (d, i) {
                return 100 + 2;
            })
            .attr("dx", 0)
            .attr("dy", 18)
            .attr("font-size", 10)
            .attr("fill", "black")
            .text(function (d, i) {
                return d['dcprivence'] + ":" + d['count'];
            })

    }

    function changeValue(obj) {
        //alert(obj.getAttribute('data'));
    }

    function createTable(dataset) {
        $("#container").empty();
        var table = $("<table border=\"1\" class=\"table table-bordered table-hover definewidth m10\"> ");
        table.appendTo($("#container"));
        var tr = $("<tr></tr>");
        tr.appendTo(table);

        var td1 = $("<td>" + '省份' + "</td>");
        td1.appendTo(tr);
        var td2 = $("<td>" + '成功数' + "</td>");
        td2.appendTo(tr);
        var td3 = $("<td>" + '限量' + "</td>");
        td3.appendTo(tr);
        tr.appendTo(table);
        for (var key in dataset) {

            var tr = $("<tr></tr>");
            tr.appendTo(table);

            var td1 = $("<td>" + dataset[key]['dcprivence'] + "</td>");
            td1.appendTo(tr);
            var td2 = $("<td>" + dataset[key]['count'] + "</td>");
            td2.appendTo(tr);
            var td3 = $("<td><input type='text' class='currentPage' name='max' id='" + dataset[key]['dcprivence'] + "' value='" + dataset[key]['max'] + "'/></td>");

            td3.appendTo(tr);
            var td4 = $("<td><button class='buttons'  data='" + dataset[key]['dcprivence'] + "'>设置</button></td>");

            td4.appendTo(tr);
            tr.appendTo(table);
        }

        $("#container").append("</table>");
        $('.buttons').on('click', function (e) {
            var key = e.target.getAttribute("data")
            var a = $("#" + key);
            var url = "http://localhost:9090/cp/unicom.php/province/setLimit";
            $.ajax(url, {
                type: 'post',
                data: {"privence": key, "limit": a.val()},
                error: function (request) {
                    alert("Connection error" + request.error);
                },
                success: function (data) {
                    alert("设置成功");
                }
            });


        },);

    }

    function drawMonthText(data) {
        var svg = d3.select("#month_text")
            .append('svg')
            .attr("width", 800)
            .attr("height", 50);
        //.attr('backgroundColor',"red");
        svg.selectAll("text")
            .data(data)
            .enter()
            .append('text')
            .attr("x", function (d, i) {
                return i * 50;
            })
            .attr("y", function (d, i) {
                return 0;
            })
            .attr("dx", 5)
            .attr("dy", 18)
            .attr("font-size", 10)
            .attr("fill", "black")
            .text(function (d, i) {
                var text = "";
                switch (d['other']) {
                    case "0":
                        text = "包月:" + d['count'];
                        break;
                    case "1":
                        text = "点播:" + d['count'];
                        break;
                    case "-1":
                        text = "退订:" + d['count'];
                        break;
                    default:
                        // statements_def
                        break;
                }


                return text;


            })

    }


    function drawTable(dataset) {
        $("#container").empty();
        var table = $("<table border=\"1\" class=\"table table-bordered table-hover definewidth m10\"> ");
        table.appendTo($("#container"));
        var tr = $("<tr></tr>");
        tr.appendTo(table);

        var td1 = $("<td>" + '省份' + "</td>");
        td1.appendTo(tr);
        var td2 = $("<td>" + '数量' + "</td>");
        td2.appendTo(tr);
       
       
        tr.appendTo(table);
        for (var key in dataset) {

            var tr = $("<tr></tr>");
            tr.appendTo(table);

            var td1 = $("<td>" + dataset[key]['dcprivence'] + "</td>");
            td1.appendTo(tr);
            var td2 = $("<td>" + dataset[key]['count'] + "</td>");
            td2.appendTo(tr);
            tr.appendTo(table);
        }

        $("#container").append("</table>");
    }

    window['fighting']['drawRect'] = drawRect;
    window['fighting']['drawMonthText'] = drawMonthText;
    window['fighting']['dateFormat'] = dateFormat;
    window['fighting']['createTable'] = createTable;
    window['fighting']['changeValue'] = changeValue;

});
